package com.ivo.web.form.parser;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.ivo.util.StringUtil;
import com.ivo.web.form.def.CheckBoxGroup;
import com.ivo.web.form.def.DateField;
import com.ivo.web.form.def.Form;
import com.ivo.web.form.def.NumbericField;
import com.ivo.web.form.def.RadioGroup;
import com.ivo.web.form.def.SubTitle;
import com.ivo.web.form.def.Table;
import com.ivo.web.form.def.TableLayout;
import com.ivo.web.form.def.TextArea;
import com.ivo.web.form.def.TextField;
import com.ivo.web.form.intf.Component;

public class FormDecoder
{
	private static final String FORM_LAYOUT_TABLE = "TABLELAYOUT";
	private Form form;

	public FormDecoder(Form form)
	{
		this.form = form;
	}

	public String parseHtml()
	{
		String html = null;
		//-----------------------------------------------------------------------------
		html = decodeForm( form );
		//-----------------------------------------------------------------------------
		return html;
	}

	public String decodeForm(Form form)
	{
		Document document = DocumentHelper.createDocument();
		Element fm = document.addElement( "form" );
		fm.addAttribute( "id", "basic-form" );
		fm.addAttribute( "name", "basic-form" );
		//-----------------------------------------------------------------------------
		if( form.getLayout() instanceof TableLayout ) {
			TableLayout layout = (TableLayout) form.getLayout();
			int cols = layout.getCols();
			//------------------------------------------------------------------------------------
			double width = 0.0000;
			width = 1 / ( 7 * cols - 1 );
			//------------------------------------------------------------------------------------
			Element table = fm.addElement( "table" );
			table.addAttribute( "style", "width:96%;" );
			//------------------------------------------------------------------------------------
			Element row = table.addElement( "tr" );
			for( int i = 0; i < ( ( 2 * cols ) - 1 ); i++ ) {
				if( i % 2 == 0 ) {
					Element cell1 = row.addElement( "td" );
					cell1.addAttribute( "style", "width: " + ( width * 200 ) + "%;" );
					Element cell2 = row.addElement( "td" );
					cell2.addAttribute( "style", "width: " + ( width * 400 ) + "%;" );
				} else {
					Element cell = row.addElement( "td" );
					cell.addAttribute( "style", "width: " + ( width * 100 ) + "%;" );
				}
			}
			//-----------------------------------------------------------------------------
			decodeFormFields( cols, table, form.getFields() );
			//-----------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------

		//-----------------------------------------------------------------------------
		return fm.asXML();
	}

	public void decodeFormFields(int cols, Element table, List<Component> components)
	{
		if( components == null ) return;
		//-----------------------------------------------------------------------------
		Element row = null;
		for( int i = 0; i < components.size(); i++ ) {
			if( i % cols == 0 ) row = table.addElement( "tr" );
			//-------------------------------------------------------------------------
			Component component = components.get( i );
			//-------------------------------------------------------------------------
			if( component instanceof Table ) {

			} else if( component instanceof TextField ) {
				Element cell = null;
				String label = ( (TextField) component ).getLabel();
				String value = ( (TextField) component ).getValue();
				int length = ( (TextField) component ).getMaxLength();
				if( StringUtil.notEmpty( label ) ) {
					cell = row.addElement( "td" );
					cell.addAttribute( "style", "text-align:right;width:15%;" );
					//-----------------------------------------------------------------------------
					Element eLabel = cell.addElement( "label" );
					eLabel.addAttribute( "for", component.getName() );
					eLabel.addText( label + "" );
				}
				cell = row.addElement( "td" );
				//-----------------------------------------------------------------------------
				Element field = cell.addElement( "input" );
				field.addAttribute( "type", "text" );
				field.addAttribute( "validator", "type:string;" );
				field.addAttribute( "class", "text ui-widget-content ui-corner-all" );
				field.addAttribute( "style", "width:72%;" );
				field.addAttribute( "id", component.getId() );
				field.addAttribute( "name", component.getName() );
				if( StringUtil.notEmpty( value ) ) field.addAttribute( "value", value );
				if( length > 0 ) field.addAttribute( "length", length + "" );
				
			} else if( component instanceof NumbericField ) {
				Element cell = null;
				String label = ( (NumbericField) component ).getLabel();
				String value = ( (NumbericField) component ).getValue();
				if( StringUtil.notEmpty( label ) ) {
					cell = row.addElement( "td" );
					cell.addAttribute( "style", "text-align:right;width:15%;" );
					//-----------------------------------------------------------------------------
					Element eLabel = cell.addElement( "label" );
					eLabel.addAttribute( "for", component.getName() );
					eLabel.addText( label + "" );
				}
				cell = row.addElement( "td" );
				//-----------------------------------------------------------------------------
				Element field = cell.addElement( "input" );
				field.addAttribute( "type", "text" );
				field.addAttribute( "validator", "type:string;" );
				field.addAttribute( "class", "text ui-widget-content ui-corner-all" );
				field.addAttribute( "style", "width:72%;" );
				field.addAttribute( "id", component.getId() );
				field.addAttribute( "name", component.getName() );
				if( StringUtil.notEmpty( value ) ) field.addAttribute( "value", value );
			} else if( component instanceof DateField ) {

			} else if( component instanceof TextArea ) {

			} else if( component instanceof RadioGroup ) {

			} else if( component instanceof CheckBoxGroup ) {

			} else if( component instanceof SubTitle ) {

			}
		}
	}
}